#pragma once

namespace Aron
{
	class Date
	{
	public:
		Date(int year = 1970, int month = 1, int day = 1)
			: _year(year)
			, _month(month)
			, _day(day)
		{}
		bool operator<(const Date& d)const
		{
			return (_year < d._year) ||
				(_year == d._year && _month < d._month) ||
				(_year == d._year && _month == d._month && _day < d._day);
		}
		bool operator>(const Date& d)const
		{
			return (_year > d._year) ||
				(_year == d._year && _month > d._month) ||
				(_year == d._year && _month == d._month && _day > d._day);
		}
		friend std::ostream& operator<<(std::ostream& _cout, const Date& d)
		{
			_cout << d._year << "-" << d._month << "-" << d._day;
			return _cout;
		}
	private:
		int _year;
		int _month;
		int _day;
	};

	template<class T>
	struct less
	{
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<class T>
	struct greater
	{
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T>
	struct DateLess
	{
		bool operator()(const T& x, const T& y)
		{
			return *x < *y;
		}
	};

	template<class T>
	struct DateGreater
	{
		bool operator()(const T& x, const T& y)
		{
			return *x > *y;
		}
	};

	/*template<class T>
	struct less<T*>
	{
		bool operator()(const T& x, const T& y)
		{
			return *x < *y;
		}
	};

	template<class T>
	struct greater<T*>
	{
		bool operator()(const T& x, const T& y)
		{
			return *x > *y;
		}
	};*/

	template<class T, class Container = vector<T>, class Comper = less<T>>
	class priority_queue
	{
	public:
		priority_queue()
			: _con()
		{}

		template<class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			: _con()
		{
			while (first != last)
			{
				push(*first);
				first++;
			}
		}

		bool empty() const
		{
			return _con.empty();
		}

		size_t size() const
		{
			return _con.size();
		}

		const T& top() const
		{
			return _con.front();
		}

		void push(const T& val)
		{
			_con.push_back(val);
			adjust_up(size() - 1);
		}

		void pop()
		{
			if (empty())
				return;
			std::swap(_con.front(), _con.back());
			_con.pop_back();
			adjust_down(0);
		}

	protected:
		void adjust_up(size_t child)
		{
			size_t parent = (child - 1) / 2;

			while (child != 0)
			{
				if (Comper()(_con[parent], _con[child]))
				{
					std::swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
					break;
			}
		}

		void adjust_down(size_t parent)
		{
			size_t child = parent * 2 + 1;

			while (child < size())
			{
				if (child + 1 < size() && Comper()(_con[child], _con[child + 1]))
					child++;

				if (Comper()(_con[parent], _con[child]))
				{
					std::swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
					break;
			}
		}

	private:
		Container _con;
	};

	void test_pq1()
	{
		vector<int> arr = { 27, 15, 19, 18, 28, 34, 65, 49, 25, 37 };
		priority_queue<int> pq(arr.begin(), arr.end());

		while (!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
	}

	void test_pq3()
	{
		priority_queue<Date*, vector<Date*>, DateLess<Date*>> p1;
		p1.push(new Date(2013, 4, 30));
		p1.push(new Date(2014, 5, 30));
		p1.push(new Date(2024, 5, 1));
		cout << *(p1.top()) << endl;

		priority_queue<Date*, vector<Date*>, DateGreater<Date*>> p2;
		p2.push(new Date(2013, 4, 30));
		p2.push(new Date(2014, 5, 30));
		p2.push(new Date(2024, 5, 1));
		cout << *(p2.top()) << endl;
	}
}